package com.wise.mall.admin.controller;

import cn.hutool.json.JSONUtil;
import com.wise.mall.admin.dto.response.UmsAdmin;
import com.wise.mall.admin.service.ums.AdminService;
import com.wise.mall.common.constant.AuthConstant;
import com.wise.mall.common.exception.Asserts;
import com.wise.mall.common.model.ResultCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author shiweinan
 */
@RestController
public class BaseController {

    @Autowired
    protected HttpServletRequest request;

    @Autowired
    protected AdminService adminService;

    public UmsAdmin getCurrentUser() {
        String userStr = request.getHeader(AuthConstant.USER_TOKEN_HEADER);
        if (StringUtils.isEmpty(userStr)) {
            Asserts.fail(ResultCode.UNAUTHORIZED);
        }
        UmsAdmin umsAdmin = JSONUtil.toBean(userStr, UmsAdmin.class);
        umsAdmin = adminService.loadAdminUserById(umsAdmin.getId());
        return umsAdmin;
    }
}
